package com.atguigu.day11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.SqlDialect;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

public class Flink07_HiveCataLog {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");

        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);


        //TODO 3.创建hiveCataLog
        String name = "hivecatalog";
        String defaultDatabase = "default";
        String hiveConf = "C:/conf";

        HiveCatalog hiveCatalog = new HiveCatalog(name, defaultDatabase, hiveConf);

        //注册CataLog
        tableEnv.registerCatalog("hivecatalog", hiveCatalog);

        //使用Catalog
        tableEnv.useCatalog("hivecatalog");
        tableEnv.useDatabase("default");

        //设置sql方言 1.13.0版本有bug
        tableEnv.getConfig().setSqlDialect(SqlDialect.HIVE);

        //查询hive中的数据
        tableEnv.executeSql("select * from student").print();
    }
}
